package com.atguigu.gmall.all.controller;

import com.atguigu.gmall.activity.client.ActivityFeignClient;
import com.atguigu.gmall.common.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author atguigu-mqx
 */
@Controller
public class SeckillController {

    @Autowired
    private ActivityFeignClient activityFeignClient;

    //  编写秒杀控制器！
    //  http://activity.gmall.com/seckill.html
    @GetMapping("seckill.html")
    public String seckillIndex(Model model){
        //  后台需要存储一个 ${list}
        Result result = activityFeignClient.findAll();
        model.addAttribute("list",result.getData());
        //  返回页面
        return "seckill/index";
    }

    //  th:href="'/seckill/'+${item.skuId}+'.html'"
    @GetMapping("seckill/{skuId}.html")
    public String seckillItem(@PathVariable Long skuId,Model model){
        //  需要存储一个item！
        Result result = activityFeignClient.getSeckillGoods(skuId);
        model.addAttribute("item",result.getData());
        //  返回秒杀详情页面
        return "seckill/item";
    }

    //  http://activity.gmall.com/seckill/queue.html?skuId=46&skuIdStr=c81e728d9d4c2f636f067f89cc14862c
    @GetMapping("seckill/queue.html")
    public String seckillQueue(HttpServletRequest request){
        //  ${skuId} ${skuIdStr}
        String skuId = request.getParameter("skuId");
        String skuIdStr = request.getParameter("skuIdStr");
        request.setAttribute("skuId",skuId);
        request.setAttribute("skuIdStr",skuIdStr);
        //  返回视图页面名称
        return "seckill/queue";
    }

    //  去下单页面
    @GetMapping("seckill/trade.html")
    public String trade(Model model){
        //  当前需要在后台存储 detailArrayList  userAddressList totalNum totalAmount
        //  调用秒杀活动模块的方法
        Result<Map<String, Object>> result = activityFeignClient.trade();
        if (result.isOk()){
            //  保存秒杀订单需要的数据！
            model.addAllAttributes(result.getData());
            //  返回下单成功页面！
            return "seckill/trade";
        }else {
            model.addAttribute("message","下单失败!");
            //  下单失败返回失败页面！
            return "seckill/fail";
        }
    }

}
